package com.macro.mall.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.pms.domain.AttributeCategory;
import com.macro.mall.pms.entity.AttributeCategoryEntity;
import com.macro.mall.pms.mapper.AttributeCategoryMapper;
import com.macro.mall.pms.service.AttributeCategoryService;
import com.macro.mall.pms.service.bo.InsertAttributeCategoryInput;
import com.macro.mall.pms.service.bo.PagedQueryAttributeCategoryInput;
import com.macro.mall.pms.service.bo.UpdateAttributeCategoryInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品属性分类Service
 *
 * @author xuyanjun
 * @date 2019-12-14
 */
@Slf4j(topic = "AttributeCategoryServiceImpl")
@Service
public class AttributeCategoryServiceImpl implements AttributeCategoryService {

    @Autowired
    private AttributeCategoryMapper attributeCategoryMapper;

    /**
     * 新增产品属性分类
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertAttributeCategoryInput input) {
        AttributeCategoryEntity attributeCategoryEntity = new AttributeCategoryEntity();
        attributeCategoryEntity
                .setName(input.getName())
                .setAttributeCount(input.getAttributeCount())
                .setParamCount(input.getParamCount());

        attributeCategoryMapper.insert(attributeCategoryEntity);
        return attributeCategoryEntity.getId();
    }

    /**
     * 修改产品属性分类
     *
     * @param input input
     */
    @Override
    public void update(UpdateAttributeCategoryInput input) {
        AttributeCategoryEntity attributeCategoryEntity = attributeCategoryMapper.selectById(input.getId());
        if (attributeCategoryEntity == null) {
            throw new BusinessException(String.format("未能根据Id:%s找到指定的产品属性分类。", input.getId()));
        }

        attributeCategoryEntity = new AttributeCategoryEntity();
        attributeCategoryEntity
                .setId(input.getId())
                .setName(input.getName())
                .setAttributeCount(input.getAttributeCount())
                .setParamCount(input.getParamCount())
        ;


        attributeCategoryMapper.updateById(attributeCategoryEntity);
    }

    /**
     * 删除产品属性分类
     *
     * @param id id
     */
    @Override
    public void delete(long id) {
        attributeCategoryMapper.deleteById(id);
    }

    /**
     * 根据Id获取产品属性分类
     *
     * @param id id
     * @return 产品属性分类
     */
    @Override
    public AttributeCategoryEntity getById(long id) {
        return attributeCategoryMapper.selectById(id);
    }

    /**
     * 根据批量Id获取产品属性分类
     *
     * @param batchIds 批量ids
     * @return 产品属性分类集合
     */
    @Override
    public List<AttributeCategoryEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return attributeCategoryMapper.selectBatchIds(batchIds);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的产品属性分类
     */
    @Override
    public Page<AttributeCategoryEntity> pagedQuery(PagedQueryAttributeCategoryInput input) {
        LambdaQueryWrapper<AttributeCategoryEntity> queryWrapper = Wrappers.lambdaQuery();

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<AttributeCategoryEntity>) attributeCategoryMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param attributeCategoryEntity 实体
     * @return attributeCategory
     */
    @Override
    public AttributeCategory entityToDomain(AttributeCategoryEntity attributeCategoryEntity) {
        if (attributeCategoryEntity == null) {
            return null;
        }

        AttributeCategory attributeCategory = new AttributeCategory();
        attributeCategory
                .setId(attributeCategoryEntity.getId())
                .setName(attributeCategoryEntity.getName())
                .setAttributeCount(attributeCategoryEntity.getAttributeCount())
                .setParamCount(attributeCategoryEntity.getParamCount())
        ;

        return attributeCategory;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param attributeCategoryEntities 实体
     * @return attributeCategorys
     */
    @Override
    public List<AttributeCategory> entityToDomain(List<AttributeCategoryEntity> attributeCategoryEntities) {
        List<AttributeCategory> result = new ArrayList<>();
        if (attributeCategoryEntities == null) {
            return result;
        }

        for (AttributeCategoryEntity attributeCategoryEntity : attributeCategoryEntities) {
            AttributeCategory attributeCategory = entityToDomain(attributeCategoryEntity);
            result.add(attributeCategory);
        }
        return result;
    }

}
